import { _decorator, Label, Node, Sprite, SpriteFrame, Tween, tween, v3 } from 'cc';
import { CfgItem } from '../config/CfgItem';
import { DtoReward } from '../dto/DtoReward';
import { ItemType } from '../enum/ItemType';
import { CoinMgr } from '../manager/CoinMgr';
import { ItemMgr } from '../manager/ItemMgr';
import { ResMgr } from '../manager/ResMgr';
import { InputBlock } from '../misc/InputBlock';
import { UIBase } from '../scriptBase/UIBase';
import { CoinItem } from '../uiItem/CoinItem';

const { ccclass, property } = _decorator;

@ccclass('UIGetReward')
export class UIGetReward extends UIBase {
    // @property({ type: Sprite })
    private icon: Sprite = null
    // @property({ type: Label })
    private lbCnt: Label = null
    // @property({ type: CoinItem })
    private coinItem: CoinItem = null

    private rewardArr: DtoReward[] = []

    private tw: Tween<Node>

    private curReward: DtoReward = null
    public set CurReward(reward: DtoReward) {
        this.curReward = reward
        const { itemId, cnt } = reward
        const { bundle, icon, type } = CfgItem[itemId]
        this.lbCnt.string = `x${cnt}`
        const spf: SpriteFrame = ResMgr.getSpriteFrame(bundle, icon)
        this.icon.spriteFrame = spf

        if (this.tw) this.tw.stop()
        this.tw = tween(this.icon.node)
        this.tw.set({ scale: v3() })
        this.tw.to(0.5, { scale: v3(1, 1, 1) }, { easing: 'backOut' })
        this.tw.start()
    }

    protected onLoad(): void {
        // this.icon = this.findComp('Icon', Sprite)
        // this.lbCnt = this.findComp('LbCnt', Label)
        // this.coinItem = this.findComp('CoinItem', CoinItem)

        this.icon = this.node.getChildByName('Icon').getComponent(Sprite);
        this.lbCnt = this.node.getChildByName('LbCnt').getComponent(Label); 
        this.coinItem = this.node.getChildByName('CoinItem').getComponent(CoinItem);
    }

    public onOpen(data?: any): void {
        this.coinItem.init(CoinMgr.CurCoin)
        this.rewardArr = data
        this.CurReward = this.rewardArr.shift()
    }

    public onClose(data?: any): void {

    }

    protected async onBtnTripleGetClick() {
        const len: number = this.rewardArr.length
        if (len <= 0) {
            await this.getItem(3)
            this.close()
        } else {
            await this.getItem(3)
            this.CurReward = this.rewardArr.shift()
        }

    }

    protected async onBtnNormalGetClick() {
        const len: number = this.rewardArr.length
        if (len <= 0) {
            await this.getItem()
            this.close()
        } else {
            await this.getItem()
            this.CurReward = this.rewardArr.shift()
        }
    }

    private getItem(time: number = 1) {
        return new Promise<void>((resolve, reject) => {
            const { itemId, cnt } = this.curReward
            const itemType: ItemType = ItemMgr.getType(itemId)

            this.lbCnt.string = `x${time * cnt}`

            ItemMgr.updateItem(itemId, time * cnt)

            switch (itemType) {
                case ItemType.Coin:
                    this.coinItem.updateCoin(this.coinItem.Coin + time * cnt)
                    InputBlock.setActive(2)
                    this.scheduleOnce(() => {
                        resolve()
                    }, 2)
                    break;
                case ItemType.Skill:
                    resolve()
                    break
                case ItemType.Collection:
                    resolve()
                default:
                    break;
            }
        })
    }

}


